package com.neu.flashmall.product.service.impl;


import com.neu.flashmall.product.entity.PmsProductAttrValue;
import com.neu.flashmall.product.mapper.PmsProductAttrValueMapper;
import com.neu.flashmall.product.service.PmsProductAttrValueService;
import com.neu.flashmall.product.vo.AttrListVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class PmsProductAttrValueServiceImpl implements PmsProductAttrValueService {

    @Resource
    private PmsProductAttrValueMapper pmsProductAttrValueMapper;

    @Override
    public String addAttrValue(PmsProductAttrValue pmsProductAttrValue) {
        pmsProductAttrValue.setQuickShow(0);
        int result = pmsProductAttrValueMapper.insertSelective(pmsProductAttrValue);
        if(result == 1){
            return "添加属性值成功";
        }
        return "数据库异常";
    }



    @Override
    public String deleteAttrValue(Long id) {
        int result = pmsProductAttrValueMapper.deleteByPrimaryKey(id);
        if(result == 1) return "删除成功";
        return "数据库异常";
    }

    @Override
    public String updateAttrValue(PmsProductAttrValue pmsProductAttrValue) {
        if(pmsProductAttrValue.getId() == null) return "id不能为空";
        int result = pmsProductAttrValueMapper.updateByPrimaryKeySelective(pmsProductAttrValue);
        if(result == 1) return "更新成功";
        return "数据库异常";
    }

    @Override
    public int canDeleteAttr(Long id) {
        return pmsProductAttrValueMapper.selectByAttrId(id).size();
    }

    @Override
    public int deleteBySpuId(Long id) {
        return this.pmsProductAttrValueMapper.deleteBySpuId(id);
    }

    @Override
    public List<Long> getAddAttrId(Long spuId) {
        return this.pmsProductAttrValueMapper.selectAddAttrId(spuId);
    }

    @Override
    public List<PmsProductAttrValue> getBySpuId(Long spuId) {
        return pmsProductAttrValueMapper.getBySpuId(spuId);
    }

    @Override
    public List<PmsProductAttrValue> selectByAttrId(Long attrId) {
        return pmsProductAttrValueMapper.selectByAttrId(attrId);
    }

    @Override
    public AttrListVo getAttrListVo(Long id){
        AttrListVo attrListVo = new AttrListVo();
        List<AttrListVo.Attr> list = new ArrayList<>();
        List<PmsProductAttrValue> attrList = pmsProductAttrValueMapper.getBySpuId(id);
        for(PmsProductAttrValue pmsProductAttrValue : attrList){
            AttrListVo.Attr one = new AttrListVo.Attr();
            one.setAttrName(pmsProductAttrValue.getAttrName());
            one.setAttrValue(pmsProductAttrValue.getAttrValue());
            list.add(one);
        }
        attrListVo.setAttrList(list);
        return attrListVo;
    }
}
